package com.qiyuesuo.controller;

import com.qiyuesuo.service.FileService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/file")
public class FileController {

    private final Logger logger = LoggerFactory.getLogger(FileController.class);

    @Autowired
    private FileService fileService;

    @PostMapping("/upload")
    public String upload(String accessToken, String file, String username)  {
        try {
            if (!fileService.valid(username, accessToken)) {
               return "ERROR";
            }
            return fileService.upload(file);
        } catch (Exception e) {
            logger.error("文件上传接口出现异常, {}", e.getMessage());
        }
        return "ERROR";
    }

    @GetMapping("/fetch")
    public String fetch(String accessToken, String fileId, String username) {
        try {
            if (!fileService.valid(username, accessToken)) {
                return "ERROR";
            }
            return fileService.fetch(fileId);
        } catch (Exception e) {
            logger.error("文件下载接口出现异常, {}", e.getMessage());
        }
        return "ERROR";
    }
}
